//
//  ViewController.swift
//  SNLoadAnimation
//
//  Created by Sunny on 26/7/18.
//  Copyright © 2018年 Sunny. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
  
    var qkLoadingAnimView:UIImageView?;
    var fangAnimView:UIImageView?;
    var isStarted:Bool = false;

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        view.backgroundColor = UIColor.white;
        
        self.combineAnim();

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func startOrFinishBtnClick(_ sender: Any) {
          self.combineAnim();
    }
    
    
    func setupQingkeLoading(repeatCount:Float) -> Void {
        
        let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        tempView.center = CGPoint(x: 100, y: 300);
        let bezierPath = UIBezierPath(ovalIn: tempView.frame)
        
        qkLoadingAnimView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 39))
        qkLoadingAnimView!.image = UIImage(named: "qk365");
        view.addSubview(qkLoadingAnimView!)
        
       let qingkeAnim = CAKeyframeAnimation(keyPath: "position")
        qingkeAnim.duration = 1.5
        qingkeAnim.path = bezierPath.cgPath
        qingkeAnim.calculationMode = kCAAnimationPaced
        qingkeAnim.fillMode = kCAFillModeForwards
//        orbitAnim.repeatCount = Float.infinity
        qingkeAnim.repeatCount = repeatCount;
        qingkeAnim.delegate = self;
        //        orbitAnim.rotationMode = kCAAnimationRotateAutoReverse
        qkLoadingAnimView!.layer.add(qingkeAnim, forKey: "orbitAnim")
        
//        let shapeLayer = CAShapeLayer()
//        shapeLayer.strokeColor = UIColor.purple.cgColor
//        shapeLayer.fillColor = UIColor.clear.cgColor
//        shapeLayer.lineWidth = 0.5
//        shapeLayer.lineJoin = kCALineJoinRound
//        shapeLayer.lineCap = kCALineCapRound
//        shapeLayer.path = bezierPath.cgPath
//        view.layer.addSublayer(shapeLayer)
    }
    
    func setupFangLoading(repeatCount:Float) -> Void {
        /**------------------------移动动画-------------------------------------*/
        fangAnimView = UIImageView(frame: CGRect(x: 0, y:  0, width: 40, height: 40))
//        fangAnimView!.center = CGPoint(x: 100, y:  300)
        fangAnimView!.image = UIImage(named: "fang");
        //        animView.backgroundColor = UIColor.red
        view.addSubview(fangAnimView!)
        
        
        
        let animationsMuArr :NSMutableArray = NSMutableArray();

        let moveAnim = CABasicAnimation(keyPath: "position")
        moveAnim.fromValue = NSValue(cgPoint: CGPoint(x: 100, y: 400))
        moveAnim.toValue = NSValue(cgPoint: CGPoint(x: 100, y: 200))
        moveAnim.duration = 1.5
//        moveAnim.repeatCount = Float.infinity
        moveAnim.repeatCount = repeatCount;
        moveAnim.autoreverses = true
        // 以下两句 控制View动画结束后，停留在动画结束的位置
        //        moveAnim.isRemovedOnCompletion = false
        //        moveAnim.fillMode = kCAFillModeForwards
        animationsMuArr.add(moveAnim);
//        animView.layer.add(moveAnim, forKey: "moveAnim")
        
        
         /**------------------------旋转动画-------------------------------------*/
        let rotationAnimZ = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimZ.beginTime = 0.0
//        rotationAnimZ.toValue = 2 * M_PI
        rotationAnimZ.toValue = 2 * Double.pi;
        rotationAnimZ.duration = 1.5
//        rotationAnimZ.repeatCount = Float.infinity
        rotationAnimZ.repeatCount = repeatCount;
        animationsMuArr.add(rotationAnimZ);
        
       let fangAnimGroup = CAAnimationGroup();
        fangAnimGroup.animations = animationsMuArr as? [CAAnimation];
//        fangAnimGroup!.duration = CFTimeInterval(2*repeatCount);
        fangAnimGroup.duration = 3;
        fangAnimGroup.isRemovedOnCompletion = false;
        fangAnimGroup.repeatCount = repeatCount;
        fangAnimGroup.fillMode = kCAFillModeForwards;
        fangAnimGroup.delegate = self;
        
//        animView.layer.add(rotationAnimZ, forKey: "rotationAnimZ")
        fangAnimView!.layer.add(fangAnimGroup, forKey: "fangAnimGroup");
        
    }
    
    
    func combineAnim() -> Void {
        self.setupQingkeLoading(repeatCount: 5);
    }
    

//    func stopAnim() -> Void {
//        qkLoadingAnimView?.layer.removeAllAnimations();
//        fangAnimView?.layer.removeAllAnimations();
//
//        isStarted = false;
//    }

}

extension ViewController:CAAnimationDelegate{
     public func animationDidStart(_ anim: CAAnimation){
       
    }
    
    
    /* Called when the animation either completes its active duration or
     * is removed from the object it is attached to (i.e. the layer). 'flag'
     * is true if the animation reached the end of its active duration
     * without being removed. */
    
   
    public func animationDidStop(_ anim: CAAnimation, finished flag: Bool){
   
        if(anim.isKind(of: CAKeyframeAnimation.self)){//青客loading
            self.setupFangLoading(repeatCount: 5);
            qkLoadingAnimView?.isHidden = true;
            fangAnimView?.isHidden = false;
            return ;
        }

        if anim.isKind(of: CAAnimationGroup.self){//房子动画
            self.setupQingkeLoading(repeatCount: 5);
            qkLoadingAnimView?.isHidden = false;
            fangAnimView?.isHidden = true;
            return ;
        }
        
    }
}

